#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QSettings>
#include <QLabel>
#include <QMessageBox>

#include <functional>
#include <QPushButton>
#include <QInputDialog>
#include <QGridLayout>
#include <QProgressDialog>
#include <QThread>
#include <QMessageBox>
#include <list>
#include <QDebug>

using namespace std;

int MainWindow::M_count = 0;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    mSetting = new QSettings("QT Settings","Main_Window");

    loadMenu();
    loadStatusBar();
    loadControl();

    string colorStr="";
    if(loadValue("gcolor",colorStr))
        ui->groupBox->setStyleSheet("QGroupBox {border:3px solid"+QString(colorStr.c_str())+";}");
    string fontstr="";
    if(loadValue("font",fontstr))
    {
        QFont font;
        font.fromString(fontstr.c_str());
        this->setFont(font);
    }

    //多文档
    setAttribute(Qt::WA_DeleteOnClose);
    QObject::connect(ui->pbtn_MulDoc,&QPushButton::clicked,[this](){ newMultiDoc(); });
}

MainWindow::~MainWindow()
{
    delete ui;
    delete mSetting;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(this->mCurrent>0)
    {
        event->accept();
        return;
    }
    auto rbtn = QMessageBox::question(this,"提示","确认退出",QMessageBox::Yes|QMessageBox::No);
    if(rbtn == QMessageBox::Yes)
    {
        //TODO:保存窗口数据
        event->accept();
    }
    else
    {
        event->ignore();
    }
}


void MainWindow::loadMenu()
{
    auto act1 = new QAction(tr("&Recent File..."),this);
    act1->setIcon(QIcon(":/bg.jpg"));
    act1->setShortcut(tr("Ctrl+H"));
    act1->setStatusTip(tr("历史记录..."));
    QObject::connect(act1,&QAction::triggered,[&]()
    {
        M_ShowStatusMsg("Clicked His");
        QMessageBox::information(this,"warn","历史记录",QMessageBox::Ok);
    });
    menuBar()->addAction(act1);
}

void MainWindow::loadStatusBar()
{
    auto info = new QLabel(" W999 ");
    info->setText("Ready");
    info->setAlignment(Qt::AlignHCenter);
    info->setMinimumSize(info->sizeHint());
    auto l = [&](const char* msg)
    {
        info->setText(msg);
    };
    M_ShowStatusMsg = l;
    statusBar()->addWidget(info);
}

void MainWindow::loadControl()
{
    list<QPushButton*> lis;
    auto btn = new QPushButton("QInputDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQInputDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QProgressDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQProgressDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QMessageBox");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQMessageBox();});
    lis.push_back(btn);

    btn = new QPushButton("QErrorMessage");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQErrorMessage();});
    lis.push_back(btn);

    btn = new QPushButton("QColorDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQColorDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QFontDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQFontDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QPageSetupDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQPageSetupDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QFilieDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQFilieDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QPrintDialog");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQPrintDialog();});
    lis.push_back(btn);

    btn = new QPushButton("QWizard");
    QObject::connect(btn,&QPushButton::clicked,[this](){runQWizard();});
    lis.push_back(btn);

    auto grid = new QGridLayout();
    for(auto it=lis.begin();it!=lis.end();it++)
        grid->addWidget(*it);
    ui->groupBox->setLayout(grid);
}

void MainWindow::runQInputDialog()
{
    int v0 = QInputDialog::getInt(this,"输入年代","1800~2077",2025,1800,2077,10);
    int v1 = QInputDialog::getDouble(this,"输入距离","-1E8~1E8",3.1415,-1E8,1E8,1E3);
    QStringList lis;
    lis.push_back("Sun");
    lis.push_back("Moon");
    lis.push_back("Earth");
    lis.push_back("Io");
    auto v2 = QInputDialog::getItem(this,"输入类型","天体",lis,2,false);
}

void MainWindow::runQProgressDialog()
{
    auto diag = new QProgressDialog("载入数据","取消",0,100,this);
    diag->resize(200,100);
    diag->setWindowModality(Qt::WindowModal);//
    diag->show();
    for(int i=0;i<diag->maximum();i++)
    {
        diag->setValue(i);
        QThread::msleep(50);
        if(diag->wasCanceled())
            break; //用户点击取消按钮
    }
    delete diag;
}

void MainWindow::runQMessageBox()
{
    QMessageBox::information(this,"Info","Info");
}

#include <QErrorMessage>

void MainWindow::runQErrorMessage()
{
    auto msg = new QErrorMessage(this);
    msg->setWindowTitle("my error");
    msg->showMessage("Error","1024");

}
#include <QColorDialog>
void MainWindow::runQColorDialog()
{
    auto diag = new QColorDialog(this);
    diag->setWindowTitle("选择颜色");
    diag->showNormal();
    QObject::connect(diag,&QColorDialog::colorSelected,[&]()
    {
        QColor color = diag->selectedColor();
        //使用样式表（StyleSheet） 设置GroupBox的边框颜色
        ui->groupBox->setStyleSheet("QGroupBox {border:3px solid"+color.name()+";}");
        storeValue("gcolor",color.name().toStdString());
    });
}
#include <QFontDialog>
void MainWindow::runQFontDialog()
{
    bool ok;
    auto f = this->font();
    auto font = QFontDialog::getFont(&ok,f,this);
    if(ok)
        this->setFont(font);
    storeValue("font",font.toString().toStdString());
}

#include <QPageSetupDialog>
#include <QPrinter>
void MainWindow::runQPageSetupDialog()
{
    QPrinter printer;
    auto dialog = new QPageSetupDialog(&printer,this);
    if(dialog->exec()==QDialog::Accepted)
    {
        //用户接受更改
    }
    else
    {
        //用户取消更改
    }
    delete dialog;
}

#include <QFileDialog>
void MainWindow::runQFilieDialog()
{
    auto img = QFileDialog::getOpenFileName(this,
                                            "Open Image",
                                            ".",
                                            "图片文件(*.png *.jpg *.bmp *.jpeg)");
    qDebug()<<"图片文件:"<<img;

}

#include <QPrintDialog>
void MainWindow::runQPrintDialog()
{
    QPrinter printer;
    auto diag = new QPrintDialog(&printer,this);
    if(diag->exec()==QDialog::Accepted)
    {

    }
    else
    {

    }
}

#include <QWizard>
void MainWindow::runQWizard()
{
    auto wizard = new QWizard(this);

    auto page1 = new QWizardPage();
    page1->setTitle("Step 01");
    auto lb1 = new QLabel("Step 01 - content ....");
    lb1->setWordWrap(true);
    auto layout = new QVBoxLayout();
    layout->addWidget(lb1);
    page1->setLayout(layout);
    wizard->addPage(page1);

    auto page2 = new QWizardPage();
    page2->setTitle("Step 02");
    wizard->addPage(page2);

    if(wizard->exec()==QDialog::Accepted)
    {

    }
    else
    {

    }
//    wizard->show();
}

void MainWindow::storeValue(const string &key, const string &value)
{
    mSetting->setValue(key.c_str(),value.c_str());
}

void MainWindow::storeValue(const string &key, int value)
{
    mSetting->setValue(key.c_str(),value);
}

void MainWindow::storeValue(const string &key, double value)
{
    mSetting->setValue(key.c_str(),value);
}

bool MainWindow::loadValue(const string &key, string &value)
{
    if(!mSetting->contains(key.c_str()))
        return  false;
    value = mSetting->value(key.c_str()).toString().toStdString();
    return true;
}

bool MainWindow::loadValue(const string &key, int &value)
{
    if(!mSetting->contains(key.c_str()))
        return  false;
    value = mSetting->value(key.c_str()).toInt();
    return true;
}

bool MainWindow::loadValue(const string &key, double &value)
{
    if(!mSetting->contains(key.c_str()))
        return  false;
    value = mSetting->value(key.c_str()).toDouble();
    return true;
}

void MainWindow::newMultiDoc()
{
    M_count++;
    auto win = new MainWindow();
    win->mCurrent=M_count;
    std::string tt = this->windowTitle().toStdString();
    std::string ss =std::to_string(M_count);
    win->setWindowTitle((tt+ss).c_str());
    win->show();
}

